package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.dto.YddAdvposDto;
import com.itheima.ydd.common.R;
import com.itheima.ydd.entry.YddAdvpos;
import com.itheima.ydd.service.YddAdvposService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ydd_advpos")
@AllArgsConstructor
@Slf4j
public class YddAdvposController {

    @Autowired
    private YddAdvposService yddAdvposService;

    //分页查询
    @GetMapping("/list")
    public R page(String advposdesc, String advposname, int page, int limit){
        //分页构造器
        Page<YddAdvpos> pageInfo = new Page<>(page,limit);
        //条件构造器
        LambdaQueryWrapper<YddAdvpos> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(advposname), YddAdvpos::getAdvposname,advposname);
        queryWrapper.like(StringUtils.isNotEmpty(advposdesc), YddAdvpos::getAdvposdesc,advposdesc);
        //添加排序条件
        queryWrapper.orderByDesc(YddAdvpos::getAdvpossize);
        yddAdvposService.page(pageInfo,queryWrapper);


        Page<YddAdvposDto> advposDtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo,advposDtoPage,"records");
        List<YddAdvpos> records = pageInfo.getRecords();
        List<YddAdvposDto> yddAdvpos =  new ArrayList<>();

        yddAdvpos=records.stream().map(item ->{
            YddAdvposDto yddAdvposDto = new YddAdvposDto();
            BeanUtils.copyProperties(item, yddAdvposDto);
            //给advposnum赋值
            yddAdvposDto.setAdvposnum(yddAdvposService.count(item.getId()));
            return yddAdvposDto;
                }).collect(Collectors.toList());
        advposDtoPage.setRecords(yddAdvpos);

        Map map = new HashMap<>();

        map.put("count",advposDtoPage.countId());
        map.put("totalPages",advposDtoPage.getTotal());
        map.put("pageSize",advposDtoPage.getMaxLimit());
        map.put("currentPage",advposDtoPage.getCurrent());
        map.put("data",advposDtoPage.getRecords());

        return R.success(map);
    }
    //删除数据
    @GetMapping("/del")
    public R delete(Long id){
        yddAdvposService.removeById(id);
        return R.success("删除成功");
    }
    //添加数据
    @PostMapping("/add")
    public R add(@RequestBody YddAdvpos yddAdvpo){

        yddAdvposService.save(yddAdvpo);
        return R.success("添加成功");
    }

    //回显数据
    @GetMapping("/rowInfo")
    public R update(Integer id){
        YddAdvpos adv = yddAdvposService.getById(id);

        return R.success(adv);
    }
    //修改数据
    @PostMapping("update")
    public R update(@RequestBody YddAdvpos yddAdvpo){
        yddAdvposService.updateById(yddAdvpo);
        return R.success("修改成功");
    }

    @GetMapping("/all")
    public R all(){
        LambdaQueryWrapper<YddAdvpos> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(YddAdvpos::getId);
        List<YddAdvpos> list = yddAdvposService.list(queryWrapper);
        return R.success(list);
    }

}
